/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.yczbj.ycvideoplayer.ability;

import ohos.aafwk.ability.DataAbilityHelper;
import ohos.aafwk.content.Intent;
import ohos.bundle.AbilityInfo;
import ohos.sysappcomponents.settings.SystemSettings;

import com.yc.video.bridge.ControlWrapper;
import com.yc.video.player.VideoPlayer;
import com.yc.video.player.VideoPlayerBuilder;
import com.yc.video.ui.view.BasisVideoController;

import org.yczbj.ycvideoplayer.ConstantVideo;
import org.yczbj.ycvideoplayer.ResourceTable;

import java.util.ArrayList;
import java.util.List;

/**
 * 3.3 一个页面播放多个视频
 *
 * @since 2021-05-10
 */
public class MultipleAbility extends BaseAbility {
    private static final String VOD_URL_1 = "http://vfx.mtime.cn/Video/2019/03/21/mp4/190321153853126488.mp4";
    private static final String VOD_URL_2 = ConstantVideo.VideoPlayerList.get(0);
    private VideoPlayer player1;
    private VideoPlayer player2;
    private List<VideoPlayer> mVideoViews = new ArrayList<>();
    private BasisVideoController controller1;
    private BasisVideoController controller2;

    @Override
    protected int getLayoutResId() {
        return ResourceTable.Layout_ability_multiple_play;
    }

    @Override
    protected void initComponent(Intent intent) {
        super.initComponent(intent);
        player1 = (VideoPlayer) findComponentById(ResourceTable.Id_video_player1);
        player2 = (VideoPlayer) findComponentById(ResourceTable.Id_video_player2);
        initVideoPlayer();
    }

    private void initVideoPlayer() {
        // 必须设置
        player1.setUrl(VOD_URL_1);
        VideoPlayerBuilder.Builder builder = VideoPlayerBuilder.newBuilder();
        builder.setEnableAudioFocus(false);
        VideoPlayerBuilder videoPlayerBuilder = new VideoPlayerBuilder(builder);
        player1.setVideoBuilder(videoPlayerBuilder);
        controller1 = new BasisVideoController(this);
        player1.setController(controller1);
        mVideoViews.add(player1);

        // 必须设置
        player2.setUrl(VOD_URL_2);
        VideoPlayerBuilder.Builder builder2 = VideoPlayerBuilder.newBuilder();
        builder.setEnableAudioFocus(false);
        VideoPlayerBuilder videoPlayerBuilder2 = new VideoPlayerBuilder(builder2);
        player2.setVideoBuilder(videoPlayerBuilder2);
        controller2 = new BasisVideoController(this);
        player2.setController(controller2);
        mVideoViews.add(player2);
    }

    @Override
    protected void initListener() {
        super.initListener();
    }

    @Override
    protected void onActive() {
        super.onActive();
        for (VideoPlayer vv : mVideoViews) {
            vv.pause();
        }
    }

    @Override
    protected void onInactive() {
        super.onInactive();
        for (VideoPlayer vv : mVideoViews) {
            vv.pause();
        }
    }

    @Override
    protected void onStop() {
        super.onStop();
        for (VideoPlayer vv : mVideoViews) {
            vv.release();
        }
    }

    @Override
    protected void onBackPressed() {
        ControlWrapper.defaultValue();
        for (VideoPlayer vv : mVideoViews) {
            if (vv.onBackPressed()) {
                return;
            }
        }
        super.onBackPressed();
    }

    @Override
    protected void onOrientationChanged(AbilityInfo.DisplayOrientation displayOrientation) {
        DataAbilityHelper dataAbilityHelper = DataAbilityHelper.creator(this);
        String value = SystemSettings.getValue(dataAbilityHelper, SystemSettings.General.ACCELEROMETER_ROTATION_STATUS);
        if (controller1 != null) {
            controller1.setFullScreenButton(controller1.getFullButton());
        }
        if (controller2 != null) {
            controller2.setFullScreenButton(controller2.getFullButton());
        }
        if ("1".equals(value)) {
            if (controller1 != null) {
                controller1.orientationChanged(displayOrientation);
            }
            if (controller2 != null) {
                controller2.orientationChanged(displayOrientation);
            }
        }
    }
}
